package com.easylinkin.linkappapi.openapi.controller;


import com.easylinkin.linkappapi.openapi.service.DataPushService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * <p>
 * 数据推送前端控制器
 * </p>
 *
 * @author tongjie
 * @since 2020-11-7
 */
@RestController
@Slf4j
@Api(value = "DataPushController", tags = {"datapush控制器"})
public class DataPushController {

    @Resource
    private DataPushService dataPushService;

    @ApiOperation("接收设备最新上报流水数据，以及消息类型数据包")
    @PostMapping(value = "/datapush", produces = "application/json;charset=UTF-8")
    public RestMessage datapush(@ApiParam("json数据参数") @RequestBody String json) throws IOException {
// 欧阳要求 ,调用datapush 接口能立马返回结果 ,致力于解决消息消费阻塞触发熔断
        log.info("datapush-api-被调用-线程:" + Thread.currentThread());
        long start = System.currentTimeMillis();
        dataPushService.dataPush(json);
        RestMessage r = RestBuilders.successBuilder().build();
        long use = System.currentTimeMillis() - start;
        if (use > 2000) {
            log.info("datapush超过2s,其值是: " + use + "ms，json是: " + json);
        }
        return r;
    }


}
